export function checkFile(rule: any, value: File, callback: any) {
    if (rule.fileSize && value?.size) {
        if (value.size > rule.fileSize) {
            return callback(new Error(`文件大小不能超过${MyModule.utile.formatFileSize(rule.fileSize)}`));
        }
    }
    if (rule.fileAccept && value?.name) {
        const fileAccept = value.name.substring(value.name.lastIndexOf('.'));
        if (!rule.fileAccept.includes(fileAccept)) {
            return callback(new Error(`文件格式不正确，请选择${rule.fileAccept}文件`));
        }
    }
    if (rule.dimension && value && value.type.startsWith('image')) {
        const dimension = MyModule.utile.getImageDimension(value);
        if (typeof dimension === 'number') {
            if (dimension[0] !== dimension || dimension[1] !== dimension) {
                return callback(new Error(`图片尺寸不合规范，请选择${dimension}X${dimension}图片`));
            }
        }
        if (Array.isArray(dimension) && dimension.length === 2) {
            if (dimension[0] !== dimension[0] || dimension[1] !== dimension[1]) {
                return callback(new Error(`图片尺寸不合规范，请选择${dimension[0]}X${dimension[1]}图片`));
            }
        }
    }
    callback();
}

export function transformFileRule(rule: MyComment.FormItemRule | MyComment.FormItemRule[]): MyComment.FormItemRule | MyComment.FormItemRule[] {
    if (Array.isArray(rule)) {
        for (let item of rule) {
            if (item.type === 'file') {
                item.validator = checkFile;
            }
        }
    } else {
        if (rule.type === 'file') {
            rule.validator = checkFile;
        }
    }
    return rule;
}
